<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2024/1/17
 * Time: 13:41
 *describe:****
 */

namespace app\admin\model\product;


use addons\shopro\library\Tree;
use app\admin\model\shopro\Category;
use app\admin\model\shopro\Common;
use traits\model\SoftDelete;

class GoodsModel extends Common
{
//    use SoftDelete;

    /**
     * 主键
     */
    protected $pk = 'id';

    protected $name = 'goods';

    protected $autoWriteTimestamp = true;

//    protected $deleteTime = 'deletetime';


    /**
     * status 中文
     */
    public function statusList()
    {
        return [
            'up' => '上架中',
            'down' => '已下架',
            'hidden' => '已隐藏',
        ];
    }

    /**
     * 获取当前商品所属分类的所有上级
     *
     * @param string $value
     * @param array $data
     * @return array
     */
    public function getCategoryIdsArrAttr($value, $data)
    {
        $categoryIds = $data['category_ids'] ? explode(',', $data['category_ids']) : [];

        $categoryIdsArr = [];
        $category = new \app\admin\model\product\Category();

        foreach ($categoryIds as $key => $category_id) {
            $currentCategoryIds = (new Tree($category))->getParentFields($category_id);
            if ($currentCategoryIds) {
                $categoryIdsArr[] = $currentCategoryIds;
            }
        }

        return $categoryIdsArr;
    }

}
